package com.example.database.dao;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.database.SQLiteHelper;
import com.example.entity.Todo;

/**
 * TODOs DAO object.
 *
 * @author itcuties
 */
public class TodoDAO implements IDAO<Todo> {

    // Contacts table name
    private static final String TABLE_NAME = "todos";
    private static final String KEY_ID = "_id";
    private static final String KEY_NAME = "todo";
    private String[] columns = new String[]{KEY_ID, KEY_NAME};

    //Da usare in SQLiteHelper nei metodi onCreate() ed onUpgrade()
    public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME + " TEXT NOT NULL);";
    public static final String UPGRADE_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;

    private SQLiteDatabase db;
    private SQLiteHelper dbHelper;

    public TodoDAO(Context context) {
        dbHelper = new SQLiteHelper(context);
        db = dbHelper.getWritableDatabase();
    }

    // Close the db
    public void close() {
        db.close();
    }

    /**
     * Create new TODO object
     *
     * @param todo
     */
    public void insert(Todo todo) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_NAME, todo.getText());
        // Insert into DB
        db.insert(TABLE_NAME, null, contentValues);
    }

    // Updating single todo
    public int update(Todo todo) {
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, todo.getId());
        // updating row
        return db.update(TABLE_NAME, values, KEY_ID + " = ?", new String[]{String.valueOf(todo.getId())});
    }

    /**
     * Delete TODO object
     *
     * @param todoId
     */
    public void delete(int todoId) {
        // Delete from DB where id match
        db.delete(TABLE_NAME, KEY_ID + " = " + todoId, null);
    }

    // Getting single todo
    public Todo findById(int id) {
        Cursor cursor = db.query(TABLE_NAME, columns, KEY_ID + "=?",
                new String[]{String.valueOf(id)}, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        Todo todo = new Todo();
        todo.setId(Integer.parseInt(cursor.getString(0)));
        todo.setText(cursor.getString(1));
        // return todo
        cursor.close();
        db.close();

        return todo;
    }

    /**
     * Get all TODOs.
     *
     * @return
     */
    public List<Todo> findAll() {
        List<Todo> todoList = new ArrayList<Todo>();

        // Name of the columns we want to select

        // Query the database
        Cursor cursor = db.query(TABLE_NAME, columns, null, null, null, null, null);
        cursor.moveToFirst();

        // Iterate the results
        while (!cursor.isAfterLast()) {
            Todo todo = new Todo();
            // Take values from the DB
            todo.setId(cursor.getInt(0));
            todo.setText(cursor.getString(1));

            // Add to the DB
            todoList.add(todo);

            // Move to the next result
            cursor.moveToNext();
        }

        return todoList;
    }

    public int count() {
        String countQuery = "SELECT  * FROM " + TABLE_NAME;
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }

}
